package myJs.user.workflow.featureSelection.intersect

import com.softwaremill.quicklens._
import myJs.cps._
import myJs.myPkg.reactHookForm._
import shared.pojo.WorkflowMission._
import myJs.Implicits._

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._

/** Created by yz on 3/11/2020
  */
object Cp {

  case class Props(
      fieldArray: UseFieldArrayRT,
      fsData: FsData,
      fsDataChange: FsData => SyncIO[Unit],
      showIdStr: String,
      extraData: ExtraData
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def eachDataChange(curI: Int) = (eachData: EachIntersectData) => {
        val newFsData = props.fsData.modify(_.intersect.index(curI)).setTo(eachData)
        props.fsDataChange(newFsData)
      }

    }

    val allGroups = props.extraData.groups

    props.fieldArray.fields.toList.zipWithIndex.toVdomArray { case (item, curI) =>
      val data = props.fsData.intersect.getOrElse(curI, EachIntersectData())
      val displayStr = if (props.showIdStr == data.nodeId) "block" else "none"
      div(
        key := s"${item.id}",
        className := "myArg",
        display := displayStr,
        IntersectCp.Props(
          curI = curI,
          data = data,
          dataChange = FTool.eachDataChange(curI),
          allGroups = allGroups
        ).render
      )
    }

  }

}
